home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13708 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  52 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.inap.net!news1!ind-009-237-118
  3. From: dlmiller@iquest.net (Doug Miller)
  4. Subject: Re: reversing a string
  5. X-Nntp-Posting-Host: ind-009-237-118.iquest.net
  6. Message-ID: <DpM3Kz.6t4@iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Network Services
  9. X-Newsreader: News Xpress Version 1.0 Beta #2.1
  10. References: <4k6cjl$j8f@central.server.swt.edu> <4kb1s7$6eu@ibm32.perftech.com> <829058514snz@genesis.demon.co.uk> <4ke0b9$rk5@grimsel.zurich.ibm.com>
  11. Date: Tue, 9 Apr 1996 20:31:47 GMT
  12.  
  13. wgk@zurich.ibm.com (Keith Whittingham) wrote:
  14. >Ole!  (pronounced Spanishly but written 'olleh')
  15. >
  16. >Well here's a starter - no second variable but two recursive functions.
  17. >Hopefully that can be reduced to one if my project can spare the time.
  18. >(Note to manager: don't worry, just joking).
  19. >
  20. >#include <stdio.h>
  21. >
  22. >void RecRev2(char *s)
  23. >  {
  24. >  if(s[1])
  25. >    {
  26. >    if(s[2])
  27. >      {
  28. >      RecRev2(&s[1]);
  29. >      }
  30. >    s[0] ^= s[1]; s[1] ^= s[0]; s[0] ^= s[1];
  31. >    }
  32. >  }
  33. >
  34. >void RecRev1(char *s)
  35. >  {
  36. >  if(s && *s)
  37. >    {
  38. >    RecRev2(s);
  39. >    RecRev1(&s[1]);
  40. >    }
  41. >  }
  42. >
  43. >int main(int argc, char *argv)
  44. >  {
  45. >  char *s = "Hello";
  46. >  RecRev1(s);
  47. >  puts(s);
  48. >  return 0;
  49. >  }
  50. >
  51. This breaks if the string begins and ends with the same character.
  52.